import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/OrganizationApplyRecordDto.dart';
import 'package:bxultimate/domain/application/organization/OrganizationApproveItemDto.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class ApplyRecord extends StatefulWidget {
  const ApplyRecord({super.key});

  @override
  State<ApplyRecord> createState() => _ApplyRecordState();
}

class _ApplyRecordState extends State<ApplyRecord> {
  List<OrganizationApplyRecordDto> listData = [];

  int page = 1;
  int size = 10;

  getData() async {
    listData.addAll(await OrganizationRequest.getApplyRecordList(page, size));
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      header: TDRefreshHeader(),
      onRefresh: () async {
        listData = [];
        getData();
      },
      onLoad: () async {
        page++;
        getData();
      },
      child: ListView.builder(
        itemCount: listData.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () async {
              await Get.toNamed(RouterName.organizationApproveDetail,
                  arguments: listData[index].id);
              listData = [];
              getData();
            },
            child: Padding(
              padding:
                  EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
              child: _buildExaminationItem(listData[index]),
            ),
          );
        },
      ),
    );
  }

  _buildExaminationItem(OrganizationApplyRecordDto dto) {
    return Container(
      margin: EdgeInsets.only(
          //     left: AppTheme.iDefaultPadding,
          //     right: AppTheme.iDefaultPadding,
          bottom: AppTheme.iDefaultPadding),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(8)),
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              MkLoadImage(
                Assets.organizationApprove,
                width: 40,
              ),
              Gaps.hGap12,
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '${dto.applyTypeStr}',
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  Gaps.vGap4,
                  Text(
                    '${dto.submitDate}',
                    style: TextStyle(fontSize: 12, color: Color(0xFF606266)),
                  ),
                ],
              )
            ],
          ),
          _buildStatusWidget(dto),
        ],
      ),
    );
  }

  _buildStatusWidget(dto) {
    return TDTag(dto.statusStr,theme: TDTagTheme.primary,isOutline: true,);
  }
}
